<template>
  <div v-if="visible" class="config-overlay" @click="handleOverlayClick">
    <div class="config-dialog" @click.stop>
      <div class="config-header">
        <h3>模型配置</h3>
        <button class="close-btn" @click="closeDialog">×</button>
      </div>
      
      <div class="config-content">
        <div class="config-section">
          <h4>基础配置</h4>
          <div class="form-group">
            <label for="baseURL">API基础URL:</label>
            <input
              id="baseURL"
              v-model="localConfig.baseURL"
              type="text"
              placeholder="例如: https://api.siliconflow.cn"
            />
          </div>
          
          <div class="form-group">
            <label for="apiKey">API密钥:</label>
            <input
              id="apiKey"
              v-model="localConfig.openAIApiKey"
              type="password"
              placeholder="输入您的API密钥"
            />
          </div>
          
          <div class="form-group">
            <label for="modelName">模型名称:</label>
            <input
              id="modelName"
              v-model="localConfig.modelName"
              type="text"
              placeholder="请输入模型名称"
            />
          </div>
        </div>
        
        <div class="config-section">
          <h4>高级参数</h4>
          <div class="form-group">
            <label for="temperature">温度 ({{ localConfig.temperature }}):</label>
            <input
              id="temperature"
              v-model.number="localConfig.temperature"
              type="range"
              min="0"
              max="1"
              step="0.1"
            />
            <span class="range-label">创造性：低 ← → 高</span>
          </div>
          
          <div class="form-group">
            <label for="maxTokens">最大令牌数:</label>
            <input
              id="maxTokens"
              v-model.number="localConfig.maxTokens"
              type="number"
              min="100"
              max="300000"
              placeholder="留空则使用默认值"
            />
          </div>
        </div>
      </div>
      
      <div class="config-footer">
        <button class="btn-secondary" @click="resetToDefault">重置默认</button>
        <div class="footer-buttons">
          <button class="btn-secondary" @click="closeDialog">取消</button>
          <button class="btn-primary" @click="saveConfig">保存配置</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['close', 'configUpdated'])

// 默认配置
const defaultConfig = {
  baseURL: 'https://api.siliconflow.cn',
  openAIApiKey: 'sk-kxvlqqwjbkgizqedqrcrgnaxpzuluyyjbbplygzomtsmmdmc',
  modelName: 'Pro/deepseek-ai/DeepSeek-V3',
  temperature: 0,
  maxTokens: null
}

// 本地配置副本
const localConfig = ref({ ...defaultConfig })

// 从localStorage加载配置
const loadConfig = () => {
  const saved = localStorage.getItem('ai-model-config')
  if (saved) {
    try {
      const config = JSON.parse(saved)
      localConfig.value = { ...defaultConfig, ...config }
    } catch (e) {
      console.warn('配置加载失败，使用默认配置')
    }
  }
}

// 初始加载配置
loadConfig()

// 监听对话框显示状态，重新加载配置
watch(() => props.visible, (newVal) => {
  if (newVal) {
    loadConfig()
  }
})

const closeDialog = () => {
  emit('close')
}

const handleOverlayClick = () => {
  closeDialog()
}

const saveConfig = () => {
  // 保存到localStorage
  localStorage.setItem('ai-model-config', JSON.stringify(localConfig.value))
  
  // 通知父组件配置已更新
  emit('configUpdated', { ...localConfig.value })
  
  closeDialog()
}

const resetToDefault = () => {
  localConfig.value = { ...defaultConfig }
}
</script>

<style scoped>
.config-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  backdrop-filter: blur(4px);
}

.config-dialog {
  background: white;
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  max-width: 500px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.config-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #e5e7eb;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 12px 12px 0 0;
}

.config-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s;
}

.close-btn:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

.config-content {
  padding: 24px;
}

.config-section {
  margin-bottom: 32px;
}

.config-section:last-child {
  margin-bottom: 0;
}

.config-section h4 {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 600;
  color: #374151;
  border-bottom: 2px solid #e5e7eb;
  padding-bottom: 8px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-weight: 500;
  color: #374151;
  font-size: 14px;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 10px 12px;
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  font-size: 14px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-group input[type="range"] {
  padding: 0;
  height: 6px;
  background: #e5e7eb;
  border-radius: 3px;
  -webkit-appearance: none;
}

.form-group input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #667eea;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.form-group input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #667eea;
  cursor: pointer;
  border: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.range-label {
  display: block;
  margin-top: 6px;
  font-size: 12px;
  color: #6b7280;
  text-align: center;
}

.config-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-top: 1px solid #e5e7eb;
  background-color: #f9fafb;
  border-radius: 0 0 12px 12px;
}

.footer-buttons {
  display: flex;
  gap: 12px;
}

.btn-primary,
.btn-secondary {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
  background: white;
  color: #374151;
  border: 2px solid #e5e7eb;
}

.btn-secondary:hover {
  background: #f3f4f6;
  border-color: #d1d5db;
}
</style> 